{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d3fae6ec-1f51-49bb-a92c-7d2a042e7ee6",
   "metadata": {},
   "source": [
    "# Spatial cross-section models: Application to crime data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51e3ef12-b981-4cfc-8807-acfb4e70273e",
   "metadata": {},
   "source": [
    "[![DOI](https://zenodo.org/badge/387109687.svg)](https://zenodo.org/badge/latestdoi/387109687)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d302ca7f-b0a7-4904-8864-bf5c7e8f0b55",
   "metadata": {},
   "source": [
    "> Mendez C. (2021). Spatial econometrics for cross-sectional data: Columbus crime example. DOI: [10.5281/zenodo.5151076](https://doi.org/10.5281/zenodo.5151076). Notebook available at https://deepnote.com/@carlos-mendez/STATA-Spatial-panel-data-NkfLwLfHR3SY15RKWXbeIQ.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a268f2e8-8cd0-4e04-99cd-34b4bbef0178",
   "metadata": {},
   "source": [
    "## Roadmap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cd8ae20-a2e2-47df-9a6b-56b524b3a030",
   "metadata": {},
   "source": [
    "![](https://github.com/quarcs-lab/data-open/raw/master/Columbus/columbus/SpatialEcometricsRoad.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b12af98e-540b-42b6-8391-4746abf0a06d",
   "metadata": {},
   "outputs": [],
   "source": [
    "* Clean your environment\n",
    "clear all\n",
    "macro drop _all\n",
    "set more off\n",
    "*cls\n",
    "*version 17"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "060a8f59-af6a-469a-981b-439751297e40",
   "metadata": {},
   "outputs": [],
   "source": [
    "* Install packages: esttab, estadd, eststo, estout, estpost (http://repec.sowi.unibe.ch/stata/estout/index.html)\n",
    "* net install st0085_2, from(http://www.stata-journal.com/software/sj14-2)\n",
    "* ssc install estout, replace"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34474fa5-e39b-42c8-b40e-84e7e7a3213d",
   "metadata": {},
   "source": [
    "## Import W and data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3fa7468c-a75c-4010-9cc1-0f158de46fff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "      Sp dataset: Wqueen_fromStata_spmat.dta\n",
      "Linked shapefile: <none>\n",
      "            Data: Cross sectional\n",
      " Spatial-unit ID: _ID (equal to id)\n",
      "     Coordinates: <none>\n",
      "\n",
      "\n",
      "\n",
      "Weighting matrix WqueenS_fromStata15\n",
      "---------------------------------------\n",
      "           Type |           contiguity\n",
      "  Normalization |                  row\n",
      "      Dimension |              49 x 49\n",
      "Elements        |\n",
      "   minimum      |                    0\n",
      "   minimum > 0  |                   .1\n",
      "   mean         |             .0204082\n",
      "   max          |                   .5\n",
      "Neighbors       |\n",
      "   minimum      |                    2\n",
      "   mean         |             4.816327\n",
      "   maximum      |                   10\n",
      "---------------------------------------\n"
     ]
    }
   ],
   "source": [
    "* Import .dta weights matrix with spmatrix (official function from Stata15)\n",
    "use \"https://github.com/quarcs-lab/data-open/raw/master/Columbus/columbus/Wqueen_fromStata_spmat.dta\", clear\n",
    "gen id = _n\n",
    "order id, first\n",
    "spset id\n",
    "spmatrix fromdata WqueenS_fromStata15 = v*, normalize(row) replace\n",
    "spmatrix summarize WqueenS_fromStata15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bd2bd29c-9657-4eb1-b836-115fcdc76a4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "      Sp dataset: columbusDbase.dta\n",
      "Linked shapefile: <none>\n",
      "            Data: Cross sectional\n",
      " Spatial-unit ID: _ID (equal to id)\n",
      "     Coordinates: <none>\n"
     ]
    }
   ],
   "source": [
    "* Import the dataset and set up the spatial id: https://geodacenter.github.io/data-and-lab/columbus/\n",
    "use \"https://github.com/quarcs-lab/data-open/raw/master/Columbus/columbus/columbusDbase.dta\", clear\n",
    "spset id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e95a15ab-c69f-4773-ae68-7e9b130f02ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "label var CRIME \"Crime\"\n",
    "label var INC   \"Income\"\n",
    "label var HOVAL \"House value\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6165f4f2-3b33-46c2-a122-0b6a145bb79b",
   "metadata": {},
   "source": [
    "## OLS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ad0160cd-2581-4a51-9c77-12bfd6e5b51a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "      Source |       SS           df       MS      Number of obs   =        49\n",
      "-------------+----------------------------------   F(2, 46)        =     28.39\n",
      "       Model |  7423.32674         2  3711.66337   Prob > F        =    0.0000\n",
      "    Residual |  6014.89274        46  130.758538   R-squared       =    0.5524\n",
      "-------------+----------------------------------   Adj R-squared   =    0.5329\n",
      "       Total |  13438.2195        48  279.962906   Root MSE        =    11.435\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "       CRIME | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "         INC |  -1.597311   .3341308    -4.78   0.000    -2.269881   -.9247405\n",
      "       HOVAL |  -.2739315   .1031987    -2.65   0.011    -.4816597   -.0662033\n",
      "       _cons |   68.61896   4.735486    14.49   0.000     59.08692      78.151\n",
      "------------------------------------------------------------------------------\n",
      "\n",
      "\n",
      "\n",
      "Akaike's information criterion and Bayesian information criterion\n",
      "\n",
      "-----------------------------------------------------------------------------\n",
      "       Model |          N   ll(null)  ll(model)      df        AIC        BIC\n",
      "-------------+---------------------------------------------------------------\n",
      "         OLS |         49  -207.0719  -187.3772       3   380.7545   386.4299\n",
      "-----------------------------------------------------------------------------\n",
      "Note: BIC uses N = number of observations. See [R] BIC note.\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "regress CRIME INC HOVAL\n",
    "eststo OLS\n",
    "\n",
    "estat ic\n",
    "mat s=r(S)\n",
    "quietly estadd scalar AIC = s[1,5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03091237-6d86-4aeb-990f-c8e8671bd922",
   "metadata": {},
   "source": [
    "### Moran's I test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ba09908d-ee5c-47dd-81f5-654594e20fd7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "      Source |       SS           df       MS      Number of obs   =        49\n",
      "-------------+----------------------------------   F(2, 46)        =     28.39\n",
      "       Model |  7423.32674         2  3711.66337   Prob > F        =    0.0000\n",
      "    Residual |  6014.89274        46  130.758538   R-squared       =    0.5524\n",
      "-------------+----------------------------------   Adj R-squared   =    0.5329\n",
      "       Total |  13438.2195        48  279.962906   Root MSE        =    11.435\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "       CRIME | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "         INC |  -1.597311   .3341308    -4.78   0.000    -2.269881   -.9247405\n",
      "       HOVAL |  -.2739315   .1031987    -2.65   0.011    -.4816597   -.0662033\n",
      "       _cons |   68.61896   4.735486    14.49   0.000     59.08692      78.151\n",
      "------------------------------------------------------------------------------\n",
      "\n",
      "\n",
      "Moran test for spatial dependence\n",
      "         H0: Error terms are i.i.d.\n",
      "         Errorlags:  WqueenS_fromStata15\n",
      "\n",
      "         chi2(1)      =     5.21\n",
      "         Prob > chi2  =   0.0225\n"
     ]
    }
   ],
   "source": [
    "regress CRIME INC HOVAL\n",
    "estat moran, errorlag(WqueenS_fromStata15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82c3926a-dbf2-4d75-b4c4-e8c0aaf74e92",
   "metadata": {
    "tags": []
   },
   "source": [
    "### LM tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "64bbd09c-1c87-4dc3-87ee-00b8555f67a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "The following matrices have been created:\n",
      "\n",
      "1. Imported binary weights matrix WqueenS_fromStata_spatwmat (row-standardized)\n",
      "   Dimension: 49x49\n",
      "\n",
      "2. Eigenvalues matrix eWqueenS_fromStata_spatwmat\n",
      "   Dimension: 49x1\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "spatwmat using \"https://github.com/quarcs-lab/data-open/raw/master/Columbus/columbus/Wqueen_fromStata_spmat.dta\", name(WqueenS_fromStata_spatwmat) eigenval(eWqueenS_fromStata_spatwmat) standardize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "77ef40dc-069a-452d-b10f-bff92b8c1934",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "Diagnostic tests for spatial dependence in OLS regression\n",
      "---------------------------------------------------------\n",
      "\n",
      "\n",
      "Fitted model\n",
      "------------------------------------------------------------\n",
      "CRIME = INC + HOVAL\n",
      "------------------------------------------------------------\n",
      "\n",
      "Weights matrix\n",
      "------------------------------------------------------------\n",
      "Name: WqueenS_fromStata_spatwmat\n",
      "Type: Imported (binary)\n",
      "Row-standardized: Yes\n",
      "------------------------------------------------------------\n",
      "\n",
      "Diagnostics\n",
      "------------------------------------------------------------\n",
      "Test                           |  Statistic    df   p-value\n",
      "-------------------------------+----------------------------\n",
      "Spatial error:                 |\n",
      "  Moran's I                    |     2.840      1    0.005\n",
      "  Lagrange multiplier          |     5.206      1    0.023\n",
      "  Robust Lagrange multiplier   |     0.044      1    0.834\n",
      "                               |\n",
      "Spatial lag:                   |\n",
      "  Lagrange multiplier          |     8.898      1    0.003\n",
      "  Robust Lagrange multiplier   |     3.736      1    0.053\n",
      "------------------------------------------------------------\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "quietly reg CRIME INC HOVAL\n",
    "spatdiag, weights(WqueenS_fromStata_spatwmat)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e5653cb-358e-4eee-bce4-d9c3a4de302e",
   "metadata": {},
   "source": [
    "## SAR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bd9c71f2-94a7-425b-97dc-8105ae3e2883",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  (49 observations)\n",
      "  (49 observations (places) used)\n",
      "  (weighting matrix defines 49 places)\n",
      "\n",
      "Performing grid search ... finished \n",
      "\n",
      "Optimizing concentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -182.69106  \n",
      "Iteration 1:   log likelihood = -182.67397  \n",
      "Iteration 2:   log likelihood = -182.67397  \n",
      "\n",
      "Optimizing unconcentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -182.67397  \n",
      "Iteration 1:   log likelihood = -182.67397  (backed up)\n",
      "\n",
      "Spatial autoregressive model                            Number of obs =     49\n",
      "Maximum likelihood estimates                            Wald chi2(3)  =  88.00\n",
      "                                                        Prob > chi2   = 0.0000\n",
      "Log likelihood = -182.67397                             Pseudo R2     = 0.5806\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "        CRIME | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "CRIME         |\n",
      "          INC |  -1.048728   .3304297    -3.17   0.002    -1.696359   -.4010978\n",
      "        HOVAL |  -.2663348   .0888474    -3.00   0.003    -.4404725   -.0921971\n",
      "        _cons |   45.60325   7.951826     5.73   0.000     30.01796    61.18854\n",
      "--------------+----------------------------------------------------------------\n",
      "WqueenS_fr~15 |\n",
      "        CRIME |   .4233254   .1255855     3.37   0.001     .1771824    .6694684\n",
      "--------------+----------------------------------------------------------------\n",
      "  var(e.CRIME)|   96.85718   19.77581                      64.91392    144.5193\n",
      "-------------------------------------------------------------------------------\n",
      "Wald test of spatial terms:          chi2(1) = 11.36      Prob > chi2 = 0.0007\n",
      "\n",
      "\n",
      "\n",
      "Akaike's information criterion and Bayesian information criterion\n",
      "\n",
      "-----------------------------------------------------------------------------\n",
      "       Model |          N   ll(null)  ll(model)      df        AIC        BIC\n",
      "-------------+---------------------------------------------------------------\n",
      "         SAR |         49          .   -182.674       5   375.3479    384.807\n",
      "-----------------------------------------------------------------------------\n",
      "Note: BIC uses N = number of observations. See [R] BIC note.\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "progress   : 50% 100% \n",
      "\n",
      "Average impacts                                 Number of obs     =         49\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "             |            Delta-Method\n",
      "             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "direct       |\n",
      "         INC |  -1.100895   .3302446    -3.33   0.001    -1.748163   -.4536279\n",
      "       HOVAL |  -.2795832   .0935004    -2.99   0.003    -.4628406   -.0963258\n",
      "-------------+----------------------------------------------------------------\n",
      "indirect     |\n",
      "         INC |  -.7176833   .3163904    -2.27   0.023    -1.337797   -.0975695\n",
      "       HOVAL |  -.1822627   .1083184    -1.68   0.092    -.3945629    .0300375\n",
      "-------------+----------------------------------------------------------------\n",
      "total        |\n",
      "         INC |  -1.818579   .5115003    -3.56   0.000    -2.821101   -.8160566\n",
      "       HOVAL |  -.4618459   .1818435    -2.54   0.011    -.8182526   -.1054393\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15)\n",
    "eststo SAR\n",
    "\n",
    "estat ic\n",
    "mat s=r(S)\n",
    "quietly estadd scalar AIC = s[1,5]\n",
    "\n",
    "estat impact"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "412b1a72-38be-4cb8-b9cd-3cd0a442ce67",
   "metadata": {},
   "source": [
    "## SEM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fe8b43b1-dd2b-4d36-bb79-3b048eb844cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  (49 observations)\n",
      "  (49 observations (places) used)\n",
      "  (weighting matrix defines 49 places)\n",
      "\n",
      "Performing grid search ... finished \n",
      "\n",
      "Optimizing concentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -183.79112  \n",
      "Iteration 1:   log likelihood =  -183.7495  \n",
      "Iteration 2:   log likelihood = -183.74943  \n",
      "Iteration 3:   log likelihood = -183.74943  \n",
      "\n",
      "Optimizing unconcentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -183.74943  \n",
      "Iteration 1:   log likelihood = -183.74943  (backed up)\n",
      "\n",
      "Spatial autoregressive model                            Number of obs =     49\n",
      "Maximum likelihood estimates                            Wald chi2(2)  =  30.15\n",
      "                                                        Prob > chi2   = 0.0000\n",
      "Log likelihood = -183.74943                             Pseudo R2     = 0.5362\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "        CRIME | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "CRIME         |\n",
      "          INC |  -.9573054   .3771546    -2.54   0.011    -1.696515   -.2180961\n",
      "        HOVAL |  -.3045593   .0921607    -3.30   0.001    -.4851909   -.1239276\n",
      "        _cons |   60.27947   5.904297    10.21   0.000     48.70726    71.85168\n",
      "--------------+----------------------------------------------------------------\n",
      "WqueenS_fr~15 |\n",
      "      e.CRIME |   .5467529   .1575357     3.47   0.001     .2379886    .8555173\n",
      "--------------+----------------------------------------------------------------\n",
      "  var(e.CRIME)|   97.67424   20.43916                      64.81252    147.1977\n",
      "-------------------------------------------------------------------------------\n",
      "Wald test of spatial terms:          chi2(1) = 12.05      Prob > chi2 = 0.0005\n",
      "\n",
      "\n",
      "\n",
      "Akaike's information criterion and Bayesian information criterion\n",
      "\n",
      "-----------------------------------------------------------------------------\n",
      "       Model |          N   ll(null)  ll(model)      df        AIC        BIC\n",
      "-------------+---------------------------------------------------------------\n",
      "         SEM |         49          .  -183.7494       5   377.4989    386.958\n",
      "-----------------------------------------------------------------------------\n",
      "Note: BIC uses N = number of observations. See [R] BIC note.\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "progress   : 50% 100% \n",
      "\n",
      "Average impacts                                 Number of obs     =         49\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "             |            Delta-Method\n",
      "             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "direct       |\n",
      "         INC |  -.9573054   .3771546    -2.54   0.011    -1.696515   -.2180961\n",
      "       HOVAL |  -.3045593   .0921607    -3.30   0.001    -.4851909   -.1239276\n",
      "-------------+----------------------------------------------------------------\n",
      "indirect     |\n",
      "         INC |          0  (omitted)\n",
      "       HOVAL |          0  (omitted)\n",
      "-------------+----------------------------------------------------------------\n",
      "total        |\n",
      "         INC |  -.9573054   .3771546    -2.54   0.011    -1.696515   -.2180961\n",
      "       HOVAL |  -.3045593   .0921607    -3.30   0.001    -.4851909   -.1239276\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "spregress CRIME INC HOVAL, ml errorlag(WqueenS_fromStata15)\n",
    "eststo SEM\n",
    "\n",
    "estat ic\n",
    "mat s=r(S)\n",
    "quietly estadd scalar AIC = s[1,5]\n",
    "\n",
    "estat impact"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7f2fefd-27dc-4f66-809f-07a9f0db9557",
   "metadata": {},
   "source": [
    "## SLX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a2f7c89d-a66a-4cf2-959b-3e8bfffcb280",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  (49 observations)\n",
      "  (49 observations (places) used)\n",
      "  (weighting matrix defines 49 places)\n",
      "\n",
      "Optimizing unconcentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood =  -183.9706  \n",
      "Iteration 1:   log likelihood =  -183.9706  \n",
      "\n",
      "Spatial autoregressive model                            Number of obs =     49\n",
      "Maximum likelihood estimates                            Wald chi2(4)  =  76.80\n",
      "                                                        Prob > chi2   = 0.0000\n",
      "Log likelihood = -183.9706                              Pseudo R2     = 0.6105\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "        CRIME | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "CRIME         |\n",
      "          INC |   -1.09739   .3542451    -3.10   0.002    -1.791698   -.4030821\n",
      "        HOVAL |  -.2943898   .0963324    -3.06   0.002    -.4831978   -.1055817\n",
      "        _cons |   74.55343   6.363788    11.72   0.000     62.08063    87.02622\n",
      "--------------+----------------------------------------------------------------\n",
      "WqueenS_fr~15 |\n",
      "          INC |  -1.398746    .530778    -2.64   0.008    -2.439051   -.3584399\n",
      "        HOVAL |    .214841   .1970276     1.09   0.276     -.171326    .6010079\n",
      "--------------+----------------------------------------------------------------\n",
      "  var(e.CRIME)|   106.8181    21.5805                      71.89125    158.7133\n",
      "-------------------------------------------------------------------------------\n",
      "Wald test of spatial terms:          chi2(2) = 7.31       Prob > chi2 = 0.0259\n",
      "\n",
      "\n",
      "\n",
      "Akaike's information criterion and Bayesian information criterion\n",
      "\n",
      "-----------------------------------------------------------------------------\n",
      "       Model |          N   ll(null)  ll(model)      df        AIC        BIC\n",
      "-------------+---------------------------------------------------------------\n",
      "         SLX |         49          .  -183.9706       6   379.9412   391.2921\n",
      "-----------------------------------------------------------------------------\n",
      "Note: BIC uses N = number of observations. See [R] BIC note.\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "progress   : 50% 100% \n",
      "\n",
      "Average impacts                                 Number of obs     =         49\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "             |            Delta-Method\n",
      "             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "direct       |\n",
      "         INC |   -1.09739   .3542451    -3.10   0.002    -1.791698   -.4030821\n",
      "       HOVAL |  -.2943898   .0963324    -3.06   0.002    -.4831978   -.1055817\n",
      "-------------+----------------------------------------------------------------\n",
      "indirect     |\n",
      "         INC |  -1.398746    .530778    -2.64   0.008    -2.439051   -.3584399\n",
      "       HOVAL |    .214841   .1970276     1.09   0.276     -.171326    .6010079\n",
      "-------------+----------------------------------------------------------------\n",
      "total        |\n",
      "         INC |  -2.496136   .4671431    -5.34   0.000    -3.411719   -1.580552\n",
      "       HOVAL |  -.0795488   .1966064    -0.40   0.686    -.4648903    .3057926\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "spregress CRIME INC HOVAL, ml ivarlag(WqueenS_fromStata15: INC HOVAL)\n",
    "eststo SLX\n",
    "\n",
    "estat ic\n",
    "mat s=r(S)\n",
    "quietly estadd scalar AIC = s[1,5]\n",
    "\n",
    "estat impact"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8409de4-2259-45b7-85a6-55019eb1dd00",
   "metadata": {},
   "source": [
    "## SDM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "998c27c8-a3de-42e5-8bbd-065f72f7b91a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  (49 observations)\n",
      "  (49 observations (places) used)\n",
      "  (weighting matrix defines 49 places)\n",
      "\n",
      "Performing grid search ... finished \n",
      "\n",
      "Optimizing concentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -181.63946  \n",
      "Iteration 1:   log likelihood = -181.63926  \n",
      "Iteration 2:   log likelihood = -181.63925  \n",
      "\n",
      "Optimizing unconcentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -181.63925  \n",
      "Iteration 1:   log likelihood = -181.63925  (backed up)\n",
      "\n",
      "Spatial autoregressive model                            Number of obs =     49\n",
      "Maximum likelihood estimates                            Wald chi2(5)  =  93.47\n",
      "                                                        Prob > chi2   = 0.0000\n",
      "Log likelihood = -181.63925                             Pseudo R2     = 0.6120\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "        CRIME | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "CRIME         |\n",
      "          INC |  -.9199061   .3395439    -2.71   0.007      -1.5854   -.2544122\n",
      "        HOVAL |  -.2971294   .0900249    -3.30   0.001     -.473575   -.1206838\n",
      "        _cons |   44.32001   14.18338     3.12   0.002      16.5211    72.11892\n",
      "--------------+----------------------------------------------------------------\n",
      "WqueenS_fr~15 |\n",
      "          INC |  -.5839133   .6053402    -0.96   0.335    -1.770358    .6025317\n",
      "        HOVAL |   .2576843   .1850135     1.39   0.164    -.1049356    .6203042\n",
      "        CRIME |   .4034626   .1718368     2.35   0.019     .0666687    .7402564\n",
      "--------------+----------------------------------------------------------------\n",
      "  var(e.CRIME)|   93.27224   19.17105                      62.34442    139.5427\n",
      "-------------------------------------------------------------------------------\n",
      "Wald test of spatial terms:          chi2(3) = 13.88      Prob > chi2 = 0.0031\n",
      "\n",
      "\n",
      "\n",
      "Akaike's information criterion and Bayesian information criterion\n",
      "\n",
      "-----------------------------------------------------------------------------\n",
      "       Model |          N   ll(null)  ll(model)      df        AIC        BIC\n",
      "-------------+---------------------------------------------------------------\n",
      "         SDM |         49          .  -181.6393       7   377.2785   390.5213\n",
      "-----------------------------------------------------------------------------\n",
      "Note: BIC uses N = number of observations. See [R] BIC note.\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "progress   : 50% 100% \n",
      "\n",
      "Average impacts                                 Number of obs     =         49\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "             |            Delta-Method\n",
      "             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "direct       |\n",
      "         INC |  -1.024988   .3241534    -3.16   0.002    -1.660317   -.3896588\n",
      "       HOVAL |  -.2819673   .0911938    -3.09   0.002    -.4607038   -.1032309\n",
      "-------------+----------------------------------------------------------------\n",
      "indirect     |\n",
      "         INC |  -1.495926   .6979787    -2.14   0.032    -2.863939   -.1279129\n",
      "       HOVAL |    .215844   .2797595     0.77   0.440    -.3324746    .7641626\n",
      "-------------+----------------------------------------------------------------\n",
      "total        |\n",
      "         INC |  -2.520914   .7319696    -3.44   0.001    -3.955548    -1.08628\n",
      "       HOVAL |  -.0661233    .308123    -0.21   0.830    -.6700333    .5377866\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) ivarlag(WqueenS_fromStata15: INC HOVAL)\n",
    "eststo SDM\n",
    "\n",
    "estat ic\n",
    "mat s=r(S)\n",
    "quietly estadd scalar AIC = s[1,5]\n",
    "\n",
    "estat impact"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11b57c2c-73c1-47e4-a5e3-b5554329a669",
   "metadata": {},
   "source": [
    "### Wald tests"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5627218-588d-47a3-9641-e498739729ad",
   "metadata": {},
   "source": [
    "### Reduce to OLS?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "48bae8b5-7fc0-46b0-b9d2-df9082a03e80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  (49 observations)\n",
      "  (49 observations (places) used)\n",
      "  (weighting matrix defines 49 places)\n",
      "\n",
      "Performing grid search ... finished \n",
      "\n",
      "Optimizing concentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -181.63946  \n",
      "Iteration 1:   log likelihood = -181.63926  \n",
      "Iteration 2:   log likelihood = -181.63925  \n",
      "\n",
      "Optimizing unconcentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -181.63925  \n",
      "Iteration 1:   log likelihood = -181.63925  (backed up)\n",
      "\n",
      "Spatial autoregressive model                            Number of obs =     49\n",
      "Maximum likelihood estimates                            Wald chi2(5)  =  93.47\n",
      "                                                        Prob > chi2   = 0.0000\n",
      "Log likelihood = -181.63925                             Pseudo R2     = 0.6120\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "        CRIME | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "CRIME         |\n",
      "          INC |  -.9199061   .3395439    -2.71   0.007      -1.5854   -.2544122\n",
      "        HOVAL |  -.2971294   .0900249    -3.30   0.001     -.473575   -.1206838\n",
      "        _cons |   44.32001   14.18338     3.12   0.002      16.5211    72.11892\n",
      "--------------+----------------------------------------------------------------\n",
      "WqueenS_fr~15 |\n",
      "          INC |  -.5839133   .6053402    -0.96   0.335    -1.770358    .6025317\n",
      "        HOVAL |   .2576843   .1850135     1.39   0.164    -.1049356    .6203042\n",
      "        CRIME |   .4034626   .1718368     2.35   0.019     .0666687    .7402564\n",
      "--------------+----------------------------------------------------------------\n",
      "  var(e.CRIME)|   93.27224   19.17105                      62.34442    139.5427\n",
      "-------------------------------------------------------------------------------\n",
      "Wald test of spatial terms:          chi2(3) = 13.88      Prob > chi2 = 0.0031\n"
     ]
    }
   ],
   "source": [
    "spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) ivarlag(WqueenS_fromStata15: INC HOVAL)\n",
    "* Wald test: Reduce to OLS? (NO if p < 0.05 of the spatial terms)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77021413-a61c-4a11-abd4-19b40c7be7f6",
   "metadata": {},
   "source": [
    "### Reduce to SLX?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "66efde76-a1e6-4d76-a64d-38deb07ac922",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " ( 1)  [WqueenS_fromStata15]CRIME = 0\n",
      "\n",
      "           chi2(  1) =    5.51\n",
      "         Prob > chi2 =    0.0189\n"
     ]
    }
   ],
   "source": [
    "* Wald test: Reduce to SLX? (NO if p < 0.05)\n",
    "test ([WqueenS_fromStata15]CRIME = 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15b89cef-63d2-4bb2-a834-ec88325bc14f",
   "metadata": {},
   "source": [
    "### Reduce to SAR?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fe721630-3d28-4d38-a494-bf30355d3be6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " ( 1)  [WqueenS_fromStata15]INC = 0\n",
      " ( 2)  [WqueenS_fromStata15]HOVAL = 0\n",
      "\n",
      "           chi2(  2) =    2.10\n",
      "         Prob > chi2 =    0.3494\n"
     ]
    }
   ],
   "source": [
    "* Wald test: Reduce to SAR? (NO if p < 0.05)\n",
    "test ([WqueenS_fromStata15]INC = 0) ([WqueenS_fromStata15]HOVAL = 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b312f0f4-5abe-424b-b707-3c11cfa17664",
   "metadata": {},
   "source": [
    "### Reduce to SEM?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e873f487-0a36-4c39-b2e5-5eab37edadc0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  (1)  [WqueenS_fromStata15]INC = -[WqueenS_fromStata15]CRIME*[CRIME]INC\n",
      "  (2)  [WqueenS_fromStata15]HOVAL = -[WqueenS_fromStata15]CRIME*[CRIME]HOVAL\n",
      "\n",
      "               chi2(2) =        4.08\n",
      "           Prob > chi2 =        0.1300\n"
     ]
    }
   ],
   "source": [
    "* Wald test: Reduce to SEM? (NO if p < 0.05)\n",
    "testnl ([WqueenS_fromStata15]INC = -[WqueenS_fromStata15]CRIME*[CRIME]INC) ([WqueenS_fromStata15]HOVAL = -[WqueenS_fromStata15]CRIME*[CRIME]HOVAL)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1e349d0-246d-4366-aa8f-2c2992880dcb",
   "metadata": {},
   "source": [
    "## SDEM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a5ed8a6a-00dc-475d-a783-c64249a5027e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  (49 observations)\n",
      "  (49 observations (places) used)\n",
      "  (weighting matrix defines 49 places)\n",
      "\n",
      "Performing grid search ... finished \n",
      "\n",
      "Optimizing concentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood =  -181.7792  \n",
      "Iteration 1:   log likelihood =   -181.779  \n",
      "Iteration 2:   log likelihood =   -181.779  \n",
      "\n",
      "Optimizing unconcentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood =   -181.779  \n",
      "Iteration 1:   log likelihood =   -181.779  (backed up)\n",
      "\n",
      "Spatial autoregressive model                            Number of obs =     49\n",
      "Maximum likelihood estimates                            Wald chi2(4)  =  46.68\n",
      "                                                        Prob > chi2   = 0.0000\n",
      "Log likelihood = -181.779                               Pseudo R2     = 0.6092\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "        CRIME | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "CRIME         |\n",
      "          INC |  -1.052259   .3233005    -3.25   0.001    -1.685916   -.4186011\n",
      "        HOVAL |  -.2781741    .091165    -3.05   0.002    -.4568542    -.099494\n",
      "        _cons |   73.64508   8.761175     8.41   0.000      56.4735    90.81667\n",
      "--------------+----------------------------------------------------------------\n",
      "WqueenS_fr~15 |\n",
      "          INC |  -1.204876   .5860084    -2.06   0.040    -2.353431   -.0563208\n",
      "        HOVAL |   .1312451   .2111476     0.62   0.534    -.2825966    .5450868\n",
      "      e.CRIME |   .4035821   .1773581     2.28   0.023     .0559666    .7511976\n",
      "--------------+----------------------------------------------------------------\n",
      "  var(e.CRIME)|    93.8033   19.30177                      62.67113    140.4005\n",
      "-------------------------------------------------------------------------------\n",
      "Wald test of spatial terms:          chi2(3) = 12.03      Prob > chi2 = 0.0073\n",
      "\n",
      "\n",
      "\n",
      "Akaike's information criterion and Bayesian information criterion\n",
      "\n",
      "-----------------------------------------------------------------------------\n",
      "       Model |          N   ll(null)  ll(model)      df        AIC        BIC\n",
      "-------------+---------------------------------------------------------------\n",
      "        SDEM |         49          .   -181.779       7    377.558   390.8007\n",
      "-----------------------------------------------------------------------------\n",
      "Note: BIC uses N = number of observations. See [R] BIC note.\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "progress   : 50% 100% \n",
      "\n",
      "Average impacts                                 Number of obs     =         49\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "             |            Delta-Method\n",
      "             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "direct       |\n",
      "         INC |  -1.052259   .3233005    -3.25   0.001    -1.685916   -.4186011\n",
      "       HOVAL |  -.2781741    .091165    -3.05   0.002    -.4568542    -.099494\n",
      "-------------+----------------------------------------------------------------\n",
      "indirect     |\n",
      "         INC |  -1.204876   .5860084    -2.06   0.040    -2.353431   -.0563208\n",
      "       HOVAL |   .1312451   .2111476     0.62   0.534    -.2825966    .5450868\n",
      "-------------+----------------------------------------------------------------\n",
      "total        |\n",
      "         INC |  -2.257135   .6515178    -3.46   0.001    -3.534086   -.9801833\n",
      "       HOVAL |   -.146929   .2403654    -0.61   0.541    -.6180365    .3241785\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "spregress CRIME INC HOVAL, ml ivarlag(WqueenS_fromStata15: INC HOVAL) errorlag(WqueenS_fromStata15)\n",
    "eststo SDEM\n",
    "\n",
    "estat ic\n",
    "mat s=r(S)\n",
    "quietly estadd scalar AIC = s[1,5]\n",
    "\n",
    "estat impact"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "872f81a6-e520-433a-b659-313eaeccb2d5",
   "metadata": {},
   "source": [
    "## SAC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "136ac4de-4503-4bb4-8c8e-d4961c7fac7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  (49 observations)\n",
      "  (49 observations (places) used)\n",
      "  (weighting matrix defines 49 places)\n",
      "\n",
      "Performing grid search ... finished \n",
      "\n",
      "Optimizing concentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -182.57166  \n",
      "Iteration 1:   log likelihood = -182.55505  \n",
      "Iteration 2:   log likelihood = -182.55502  \n",
      "Iteration 3:   log likelihood = -182.55502  \n",
      "\n",
      "Optimizing unconcentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -182.55502  \n",
      "Iteration 1:   log likelihood = -182.55502  (backed up)\n",
      "\n",
      "Spatial autoregressive model                            Number of obs =     49\n",
      "Maximum likelihood estimates                            Wald chi2(3)  =  57.82\n",
      "                                                        Prob > chi2   = 0.0000\n",
      "Log likelihood = -182.55502                             Pseudo R2     = 0.5793\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "        CRIME | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "CRIME         |\n",
      "          INC |  -1.042749   .3357121    -3.11   0.002    -1.700733   -.3847656\n",
      "        HOVAL |  -.2798409     .09405    -2.98   0.003    -.4641755   -.0955063\n",
      "        _cons |   47.91536   9.298756     5.15   0.000     29.69013    66.14058\n",
      "--------------+----------------------------------------------------------------\n",
      "WqueenS_fr~15 |\n",
      "        CRIME |   .3693743   .1792524     2.06   0.039     .0180459    .7207026\n",
      "      e.CRIME |   .1464169   .2997182     0.49   0.625      -.44102    .7338538\n",
      "--------------+----------------------------------------------------------------\n",
      "  var(e.CRIME)|   97.04344   19.77021                      65.09608    144.6697\n",
      "-------------------------------------------------------------------------------\n",
      "Wald test of spatial terms:          chi2(2) = 10.63      Prob > chi2 = 0.0049\n",
      "\n",
      "\n",
      "\n",
      "Akaike's information criterion and Bayesian information criterion\n",
      "\n",
      "-----------------------------------------------------------------------------\n",
      "       Model |          N   ll(null)  ll(model)      df        AIC        BIC\n",
      "-------------+---------------------------------------------------------------\n",
      "         SAC |         49          .   -182.555       6     377.11    388.461\n",
      "-----------------------------------------------------------------------------\n",
      "Note: BIC uses N = number of observations. See [R] BIC note.\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "progress   : 50% 100% \n",
      "\n",
      "Average impacts                                 Number of obs     =         49\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "             |            Delta-Method\n",
      "             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "direct       |\n",
      "         INC |  -1.080454   .3399105    -3.18   0.001    -1.746666   -.4142414\n",
      "       HOVAL |  -.2899596   .0958282    -3.03   0.002    -.4777794   -.1021397\n",
      "-------------+----------------------------------------------------------------\n",
      "indirect     |\n",
      "         INC |  -.5730615   .4220918    -1.36   0.175    -1.400346    .2542232\n",
      "       HOVAL |  -.1537916   .1161114    -1.32   0.185    -.3813657    .0737826\n",
      "-------------+----------------------------------------------------------------\n",
      "total        |\n",
      "         INC |  -1.653515   .6169502    -2.68   0.007    -2.862715   -.4443151\n",
      "       HOVAL |  -.4437511   .1749537    -2.54   0.011    -.7866542   -.1008481\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) errorlag(WqueenS_fromStata15)\n",
    "eststo SAC\n",
    "\n",
    "estat ic\n",
    "mat s=r(S)\n",
    "quietly estadd scalar AIC = s[1,5]\n",
    "\n",
    "estat impact"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56dd48fa-a580-46e3-855d-a0ad397b3a6d",
   "metadata": {},
   "source": [
    "## GNS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "17cf1cb6-79b9-4d9a-948c-6534eafd2f07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  (49 observations)\n",
      "  (49 observations (places) used)\n",
      "  (weighting matrix defines 49 places)\n",
      "\n",
      "Performing grid search ... finished \n",
      "\n",
      "Optimizing concentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -181.60541  \n",
      "Iteration 1:   log likelihood = -181.58046  \n",
      "Iteration 2:   log likelihood = -181.58014  \n",
      "Iteration 3:   log likelihood = -181.58014  \n",
      "\n",
      "Optimizing unconcentrated log likelihood:\n",
      "\n",
      "Iteration 0:   log likelihood = -181.58014  \n",
      "Iteration 1:   log likelihood = -181.58014  (backed up)\n",
      "\n",
      "Spatial autoregressive model                            Number of obs =     49\n",
      "Maximum likelihood estimates                            Wald chi2(5)  =  62.57\n",
      "                                                        Prob > chi2   = 0.0000\n",
      "Log likelihood = -181.58014                             Pseudo R2     = 0.6115\n",
      "\n",
      "-------------------------------------------------------------------------------\n",
      "        CRIME | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
      "--------------+----------------------------------------------------------------\n",
      "CRIME         |\n",
      "          INC |  -.9587964   .3565186    -2.69   0.007     -1.65756   -.2600328\n",
      "        HOVAL |  -.2890341   .0924525    -3.13   0.002    -.4702378   -.1078305\n",
      "        _cons |   53.04797    31.4573     1.69   0.092    -8.607201    114.7031\n",
      "--------------+----------------------------------------------------------------\n",
      "WqueenS_fr~15 |\n",
      "          INC |  -.7734089   .8571065    -0.90   0.367    -2.453307     .906489\n",
      "        HOVAL |   .2184638   .2304209     0.95   0.343    -.2331529    .6700806\n",
      "        CRIME |    .284377   .4183839     0.68   0.497    -.5356404    1.104394\n",
      "      e.CRIME |   .1632527   .4661928     0.35   0.726    -.7504684    1.076974\n",
      "--------------+----------------------------------------------------------------\n",
      "  var(e.CRIME)|   94.48532   19.32815                      63.27621    141.0874\n",
      "-------------------------------------------------------------------------------\n",
      "Wald test of spatial terms:          chi2(4) = 12.51      Prob > chi2 = 0.0139\n",
      "\n",
      "\n",
      "\n",
      "Akaike's information criterion and Bayesian information criterion\n",
      "\n",
      "-----------------------------------------------------------------------------\n",
      "       Model |          N   ll(null)  ll(model)      df        AIC        BIC\n",
      "-------------+---------------------------------------------------------------\n",
      "         GNS |         49          .  -181.5801       8   379.1603   394.2948\n",
      "-----------------------------------------------------------------------------\n",
      "Note: BIC uses N = number of observations. See [R] BIC note.\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "progress   : 50% 100% \n",
      "\n",
      "Average impacts                                 Number of obs     =         49\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "             |            Delta-Method\n",
      "             |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "direct       |\n",
      "         INC |  -1.032865   .3233102    -3.19   0.001    -1.666541   -.3991884\n",
      "       HOVAL |  -.2793854    .091442    -3.06   0.002    -.4586084   -.1001624\n",
      "-------------+----------------------------------------------------------------\n",
      "indirect     |\n",
      "         INC |  -1.387691   .7255571    -1.91   0.056    -2.809757    .0343749\n",
      "       HOVAL |   .1807716   .2720029     0.66   0.506    -.3523444    .7138875\n",
      "-------------+----------------------------------------------------------------\n",
      "total        |\n",
      "         INC |  -2.420556   .7598804    -3.19   0.001    -3.909894   -.9312175\n",
      "       HOVAL |  -.0986138   .2989642    -0.33   0.742    -.6845729    .4873452\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) ivarlag(WqueenS_fromStata15: INC HOVAL) errorlag(WqueenS_fromStata15)\n",
    "eststo GNS\n",
    "\n",
    "estat ic\n",
    "mat s=r(S)\n",
    "quietly estadd scalar AIC = s[1,5]\n",
    "\n",
    "estat impact"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d19bb87-5786-4a2e-b83b-1b068ca2a112",
   "metadata": {},
   "source": [
    "## Comparison"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "925f508a-f57d-4abd-8ad6-ca37cc6ee83e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<table border=\"0\" width=\"*\">\n",
       "<tr><td colspan=9><hr></td></tr>\n",
       "<tr><td>                    </td><td>         (1)              </td><td>         (2)              </td><td>         (3)              </td><td>         (4)              </td><td>         (5)              </td><td>         (6)              </td><td>         (7)              </td><td>         (8)              </td></tr>\n",
       "<tr><td>                    </td><td>         OLS              </td><td>         SAR              </td><td>         SEM              </td><td>         SLX              </td><td>         SDM              </td><td>        SDEM              </td><td>         SAC              </td><td>         GNS              </td></tr>\n",
       "<tr><td colspan=9><hr></td></tr>\n",
       "<tr><td>main                </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td></tr>\n",
       "<tr><td>Income              </td><td>      -1.597<sup>***</sup></td><td>      -1.049<sup>**</sup> </td><td>      -0.957<sup>*</sup>  </td><td>      -1.097<sup>**</sup> </td><td>      -0.920<sup>**</sup> </td><td>      -1.052<sup>**</sup> </td><td>      -1.043<sup>**</sup> </td><td>      -0.959<sup>**</sup> </td></tr>\n",
       "<tr><td>                    </td><td>     (-4.78)              </td><td>     (-3.17)              </td><td>     (-2.54)              </td><td>     (-3.10)              </td><td>     (-2.71)              </td><td>     (-3.25)              </td><td>     (-3.11)              </td><td>     (-2.69)              </td></tr>\n",
       "<tr><td colspan=9>&nbsp;</td></tr>\n",
       "<tr><td>House value         </td><td>      -0.274<sup>*</sup>  </td><td>      -0.266<sup>**</sup> </td><td>      -0.305<sup>***</sup></td><td>      -0.294<sup>**</sup> </td><td>      -0.297<sup>***</sup></td><td>      -0.278<sup>**</sup> </td><td>      -0.280<sup>**</sup> </td><td>      -0.289<sup>**</sup> </td></tr>\n",
       "<tr><td>                    </td><td>     (-2.65)              </td><td>     (-3.00)              </td><td>     (-3.30)              </td><td>     (-3.06)              </td><td>     (-3.30)              </td><td>     (-3.05)              </td><td>     (-2.98)              </td><td>     (-3.13)              </td></tr>\n",
       "<tr><td colspan=9>&nbsp;</td></tr>\n",
       "<tr><td>Constant            </td><td>       68.62<sup>***</sup></td><td>       45.60<sup>***</sup></td><td>       60.28<sup>***</sup></td><td>       74.55<sup>***</sup></td><td>       44.32<sup>**</sup> </td><td>       73.65<sup>***</sup></td><td>       47.92<sup>***</sup></td><td>       53.05              </td></tr>\n",
       "<tr><td>                    </td><td>     (14.49)              </td><td>      (5.73)              </td><td>     (10.21)              </td><td>     (11.72)              </td><td>      (3.12)              </td><td>      (8.41)              </td><td>      (5.15)              </td><td>      (1.69)              </td></tr>\n",
       "<tr><td colspan=9><hr></td></tr>\n",
       "<tr><td>WqueenS_fromStata15 </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td></tr>\n",
       "<tr><td>Crime               </td><td>                          </td><td>       0.423<sup>***</sup></td><td>                          </td><td>                          </td><td>       0.403<sup>*</sup>  </td><td>                          </td><td>       0.369<sup>*</sup>  </td><td>       0.284              </td></tr>\n",
       "<tr><td>                    </td><td>                          </td><td>      (3.37)              </td><td>                          </td><td>                          </td><td>      (2.35)              </td><td>                          </td><td>      (2.06)              </td><td>      (0.68)              </td></tr>\n",
       "<tr><td colspan=9>&nbsp;</td></tr>\n",
       "<tr><td>e.Crime             </td><td>                          </td><td>                          </td><td>       0.547<sup>***</sup></td><td>                          </td><td>                          </td><td>       0.404<sup>*</sup>  </td><td>       0.146              </td><td>       0.163              </td></tr>\n",
       "<tr><td>                    </td><td>                          </td><td>                          </td><td>      (3.47)              </td><td>                          </td><td>                          </td><td>      (2.28)              </td><td>      (0.49)              </td><td>      (0.35)              </td></tr>\n",
       "<tr><td colspan=9>&nbsp;</td></tr>\n",
       "<tr><td>Income              </td><td>                          </td><td>                          </td><td>                          </td><td>      -1.399<sup>**</sup> </td><td>      -0.584              </td><td>      -1.205<sup>*</sup>  </td><td>                          </td><td>      -0.773              </td></tr>\n",
       "<tr><td>                    </td><td>                          </td><td>                          </td><td>                          </td><td>     (-2.64)              </td><td>     (-0.96)              </td><td>     (-2.06)              </td><td>                          </td><td>     (-0.90)              </td></tr>\n",
       "<tr><td colspan=9>&nbsp;</td></tr>\n",
       "<tr><td>House value         </td><td>                          </td><td>                          </td><td>                          </td><td>       0.215              </td><td>       0.258              </td><td>       0.131              </td><td>                          </td><td>       0.218              </td></tr>\n",
       "<tr><td>                    </td><td>                          </td><td>                          </td><td>                          </td><td>      (1.09)              </td><td>      (1.39)              </td><td>      (0.62)              </td><td>                          </td><td>      (0.95)              </td></tr>\n",
       "<tr><td colspan=9><hr></td></tr>\n",
       "<tr><td>/                   </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td><td>                          </td></tr>\n",
       "<tr><td>var(e.CRIME)        </td><td>                          </td><td>       96.86<sup>***</sup></td><td>       97.67<sup>***</sup></td><td>       106.8<sup>***</sup></td><td>       93.27<sup>***</sup></td><td>       93.80<sup>***</sup></td><td>       97.04<sup>***</sup></td><td>       94.49<sup>***</sup></td></tr>\n",
       "<tr><td>                    </td><td>                          </td><td>      (4.90)              </td><td>      (4.78)              </td><td>      (4.95)              </td><td>      (4.87)              </td><td>      (4.86)              </td><td>      (4.91)              </td><td>      (4.89)              </td></tr>\n",
       "<tr><td colspan=9><hr></td></tr>\n",
       "<tr><td>AIC                 </td><td>       380.8              </td><td>       375.3              </td><td>       377.5              </td><td>       379.9              </td><td>       377.3              </td><td>       377.6              </td><td>       377.1              </td><td>       379.2              </td></tr>\n",
       "<tr><td colspan=9><hr></td></tr>\n",
       "<tr><td colspan=9>\n",
       "<i>t</i> statistics in parentheses\n",
       "<br /><sup>*</sup> <i>p</i> < 0.05, <sup>**</sup> <i>p</i> < 0.01, <sup>***</sup> <i>p</i> < 0.001\n",
       "</td></tr>\n",
       "</table>\n",
       "\n"
      ],
      "text/plain": [
       "This front-end or document format cannot display HTML"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%html\n",
    "esttab OLS SAR SEM SLX SDM SDEM SAC GNS, label stats(AIC) mtitle(\"OLS\" \"SAR\" \"SEM\" \"SLX\" \"SDM\" \"SDEM\" \"SAC\" \"GNS\") html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3085a8c7-ee10-4fd8-8dc3-4d2ab6a5f25d",
   "metadata": {},
   "outputs": [],
   "source": [
    "eststo clear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cda3f5c-1f86-4768-90ff-8c66aa3a2d97",
   "metadata": {},
   "source": [
    "The following comparison requires Stata 17.  Caution is needed as the p-values are not shown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c286b968-241a-45c5-aac2-23db0bf63cc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "  Collection: default\n",
      "   Dimension: cmdset\n",
      "       Label: Command results index\n",
      "Level labels:\n",
      "           1  \n",
      "           2  \n",
      "           3  \n",
      "           4  \n",
      "           5  \n",
      "           6  \n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "Collection: default\n",
      "      Rows: colname#result\n",
      "   Columns: cmdset\n",
      "   Table 1: 6 x 6\n",
      "\n",
      "--------------------------------------------------\n",
      "             |   SAR   SLX   SDM  SDEM   SAC   GNS\n",
      "-------------+------------------------------------\n",
      "Income       |                                    \n",
      "  b_direct   | -1.10 -1.10 -1.02 -1.05 -1.08 -1.03\n",
      "  b_indirect | -0.72 -1.40 -1.50 -1.20 -0.57 -1.39\n",
      "House value  |                                    \n",
      "  b_direct   | -0.28 -0.29 -0.28 -0.28 -0.29 -0.28\n",
      "  b_indirect | -0.18  0.21  0.22  0.13 -0.15  0.18\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "collect clear\n",
    "\n",
    "quietly spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15)\n",
    "collect: quietly estat impact\n",
    "\n",
    "quietly spregress CRIME INC HOVAL, ml ivarlag(WqueenS_fromStata15: INC HOVAL)\n",
    "collect: quietly estat impact\n",
    "\n",
    "quietly spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) ivarlag(WqueenS_fromStata15: INC HOVAL)\n",
    "collect: quietly estat impact\n",
    "\n",
    "quietly spregress CRIME INC HOVAL, ml ivarlag(WqueenS_fromStata15: INC HOVAL) errorlag(WqueenS_fromStata15)\n",
    "collect: quietly estat impact\n",
    "\n",
    "quietly spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) errorlag(WqueenS_fromStata15)\n",
    "collect: quietly estat impact\n",
    "\n",
    "quietly spregress CRIME INC HOVAL, ml dvarlag(WqueenS_fromStata15) ivarlag(WqueenS_fromStata15: INC HOVAL) errorlag(WqueenS_fromStata15)\n",
    "collect: quietly estat impact\n",
    "\n",
    "\n",
    "collect label list cmdset, all\n",
    "collect style autolevels result b_direct b_indirect  \n",
    "collect label levels cmdset 1 \"SAR\" 2 \"SLX\" 3 \"SDM\" 4 \"SDEM\" 5 \"SAC\" 6 \"GNS\"\n",
    "collect style cell, nformat(%7.2f)\n",
    "collect layout (colname#result) (cmdset) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e37a613-650e-4c1a-ba8f-af47900db9a2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "codemirror_mode": "stata",
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
